package hkr.vfu.bluecenter;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.widget.Toast;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

import hkr.vfu.bluecenter.backend.BluetoothManager;
import hkr.vfu.bluecenter.backend.BoxVO;
import hkr.vfu.bluecenter.backend.DataSingleton;
import hkr.vfu.bluecenter.swipe_fragments.SectionsPagerAdapter;
import hkr.vfu.bluecenter.swipe_fragments.UpdateUIListener;


public class BluecenterController extends ActionBarActivity implements UpdateUIListener {

    private SectionsPagerAdapter mSectionsPagerAdapter;
    private ViewPager mViewPager;
    private File file;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluecenter_main);

        file = new File(getFilesDir(), "hashMap");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        loadHashMap();
    }


    @Override
    public void updateUI() {
        mSectionsPagerAdapter.updateUI();
    }

    public void loadHashMap() {
        Object temp = null;
        try {
            FileInputStream f = new FileInputStream(file);
            ObjectInputStream s = new ObjectInputStream(f);
            temp = s.readObject();
            s.close();
        } catch (IOException e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        if (temp != null && temp instanceof HashMap) {
            DataSingleton.getInstance().setHashMap((HashMap<String, BoxVO>) temp);
        } else {
            DataSingleton.getInstance().setHashMap(new HashMap<String, BoxVO>());
        }
    }

    public void saveHashMap() {
        try {
            FileOutputStream f = new FileOutputStream(file);
            ObjectOutputStream s = new ObjectOutputStream(f);
            s.writeObject(DataSingleton.getInstance().getHashMap());
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BluetoothManager.cancelAll();
    }

    public void addBoxFragment(String boxMac) {
        mSectionsPagerAdapter.addBoxFragment(boxMac);
        mViewPager.setCurrentItem(mSectionsPagerAdapter.getCount() - 1);
    }

    public void removeBoxFragment(int pos) {
        mSectionsPagerAdapter.removeBoxFragment(pos);
        mViewPager.setCurrentItem(mSectionsPagerAdapter.getCount() - 1);
    }
}
